package work

import (
	"parking-lot-go-admin/config"
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/work"
	"strings"

	"github.com/gin-gonic/gin"
)

func List(c *gin.Context) {

	requestDataMap := make(map[string]interface{})
	c.ShouldBindJSON(&requestDataMap)

	where, page, pageSize := controller.WhereStructure(requestDataMap)

	adminId, _ := c.Get("admin_id")
	if adminId != 1 {
		where["admin_id"] = adminId
	}

	//查询信息
	var workOrderStruct work.WorkOrderStruct
	var workOrderData []work.WorkOrderStruct
	model.GetModelData(
		model.Db,
		workOrderStruct.TableName(),
		&workOrderData,
		model.QueryOptions{
			Page:       page,
			PageSize:   pageSize,
			Conditions: where,
			OrderBy:    "id desc",
			Debug:      true,
		},
	)

	systemSection := config.Cfg.Section("SYSTEM")
	imgUrl := systemSection.Key("IMAGE_URL").String()

	//数据格式化
	returnData := make([]map[string]interface{}, 0)
	for _, v := range workOrderData {

		//图片url处理
		imageArr := strings.Split(v.Image, ",")
		for kk, vv := range imageArr {
			imageArr[kk] = imgUrl + vv
		}

		returnData = append(returnData, map[string]interface{}{
			"id":         v.Id,
			"car_number": v.CarNumber,
			"park_name":  v.ParkName,
			"question":   v.Question,
			"image":      imageArr,
			"created_at": v.CreatedAt.Format("2006-01-02 15:04"),
			"handle_at":  v.HandleAt.Format("2006-01-02 15:04"),
			"answer":     v.Answer,
			"remark":     v.Remark,
			"status":     v.Status,
		})
	}

	//总数查询
	count, _ := model.CountModelData(
		model.Db,
		workOrderStruct.TableName(),
		model.QueryOptions{
			Conditions: where,
		},
	)

	controller.Success(c,
		map[string]interface{}{
			"list":        returnData,
			"total":       count,
			"pageSize":    pageSize,
			"currentPage": page,
		})
}
